﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using AutoMapper;

namespace Data
{
    public class NumberIdRepository
    {
        private static NUMBERID MapNumberIdDTOToNumberIdEF(NumberIdDTO numberidDTO)
        {
            Mapper.CreateMap<NumberIdDTO,NUMBERID>();
            return Mapper.Map<NumberIdDTO,NUMBERID>(numberidDTO);

        }
        public static int NewNumber(string tenbang)
        {
            return LastNumberByTenBang(tenbang) + 1;
        }
        public static int LastNumberByTenBang(string tenbang)
        {
            int kq = new int();
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                try
                {
                    var query = (from numberid in context.NUMBERIDList
                                where numberid.TenBang == tenbang
                                select numberid.LastNumber).First();
                    kq =(int) query;

                }
                catch
                {

                }
            }
            return kq;
        }
        public static int MaBangByTenBang(string tenbang)
        {
            int kq = new int();
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                try
                {
                    var query = (from numberid in context.NUMBERIDList
                                 where numberid.TenBang == tenbang
                                 select numberid.MaBang).First();
                    kq = (int)query;

                }
                catch
                {

                }
                return kq;
            }
        }
        public static void SetNumber(int lastnumber, string tenbang)
        {
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                try
                {

                    int ma = MaBangByTenBang(tenbang);
                    NumberIdDTO numberidDTO = new NumberIdDTO();
                    numberidDTO.MaBang = ma;
                    numberidDTO.LastNumber = lastnumber;

                    EntityKey key = context.CreateEntityKey("NUMBERIDList", new NUMBERID { MaBang = ma });
                    NUMBERID orignumberid = context.GetObjectByKey(key) as NUMBERID;
                    context.Detach(orignumberid);
                    context.Attach(orignumberid);

                    NUMBERID updatenumberid = MapNumberIdDTOToNumberIdEF(numberidDTO);
                    context.ApplyCurrentValues("NUMBERIDList", updatenumberid);
                    context.SaveChanges();
                }
                catch
                {
                }
                   
            }
        }
        public static void Update(string tenbang)
        {
            using(QuanLyNhaSachEntities context =new QuanLyNhaSachEntities())
            {
                try
                {
                    int ma=MaBangByTenBang(tenbang);
                    NumberIdDTO numberidDTO=new NumberIdDTO();
                    numberidDTO.MaBang=ma;
                    numberidDTO.TenBang = tenbang;
                    numberidDTO.LastNumber=LastNumberByTenBang(tenbang)+1;

                    EntityKey key=context.CreateEntityKey("NUMBERIDList",new NUMBERID{MaBang=ma});
                    NUMBERID orignumberid=context.GetObjectByKey(key)as NUMBERID;
                    context.Detach(orignumberid);
                    context.Attach(orignumberid);

                    NUMBERID updatenumberid=MapNumberIdDTOToNumberIdEF(numberidDTO);
                    context.ApplyCurrentValues("NUMBERIDList",updatenumberid);
                    context.SaveChanges();
                   
                }
                catch
                {
                }
            }
        }
    }
}
